
var Member = Model.extend(Member = {
    family: null,
    
    init: function(data) {
        this._super();
        
        if( data ) {
            for(var key in data) {
               this.set(key, data[key]); 
            }
            
            this.generateCalculatedFields();
        }
    },
    
    generateCalculatedFields: function() {
        var self = this;
        
        // this is needed because we are using raw sql for queries at the moment
        this.update('Member', {
            full_name: self.getFullName(),
            age: self.getAge(),
            dob: self.getDOB(),
            home_address: self.getResidentialAddress(),
            office_address: self.getBusinessAddress()
        });
    },
    
    getId: function() {
        return this.field('id');
    },
    
    getGUID: function() {
        return this.field('guid');
    },
    
    field: function(key) {
        return this.get('Member') ? this.get('Member')[key] : null;
    },
    
    getAge: function() {
        var bd = this.field('dob');
        
        if( bd ) {
            var cy = new Date().getFullYear();
            var by = bd.split("-")[0];
            var age = cy - by;
            
            // no living person is currently greater than 150, so any age above that is currently
            // invalid
            return age <= 150 ? age : '';
        }
        
        return "";
    },
    
    getFullName: function() {
        return this.field('last_name') + " " + this.field('first_name');
    },
    
    getDOB: function() {
        return this.field("dob"); //$.datepicker.format("M D Y", this.field("dob"));
    },
    
    getResidentialAddress: function() {
        var home_address = this.field("residential_address") + " " + this.field("residential_city") + " " + this.field("residential_region");
        
        if( this.field("residential_zip_code") != "") {
            home_address += ", " + this.field("residential_zip_code");
        }
        
        return home_address;
    },
    
    getBusinessAddress: function() {
        var business_address = this.field("business_address") + " " + this.field("business_city") + " " + this.field("business_region");
        
        if( this.field("business_zip_code") != "") {
            business_address += ", " + this.field("business_zip_code");
        }
        
        return business_address;
    },
    
    isActive: function() {
        var end_dt = Date.parse(this.field('membership_end_dt'));

        return this.field('is_active') == 1 && (end_dt && end_dt.isAfter(Date.today()));
    },
    
    /**
     * Sends the member records to the server and saves them to the database
     */
    save: function() {
        var data = JSON.stringify([this.getRecords()]);
        var url = $("#save-member-records-url").val();
        var query = "data=" + encodeURIComponent(data);

        $.ajax({
            url: url,
            data: query,
            type: 'post',
            dataType: 'json',
            success: function(data) {
                console.log(data);
            },
            error: function(xhr) {
                console.log(xhr.responseText);
                notifications.showAlert("Data Save Failed", "Failed To Process and Save Data. See Console For Error", notifications.CRITICAL)
            }
        });
    },
    
    removeFromDB: function() {

        var query = "member_id=" + this.getId();
        var url = $("#remove-added-member-url").val();

        $.ajax({
            'url': url,
            'data': query,
            'type': 'POST',
            success: function() {

            },
            error: function(xhr, text) {
                console.log(xhr.responseText);
            }
        });

    }
});
